var ClubDataOverviewTab = Item.extend({
    _ccsFileName: res.ClubDataOverviewTab_json,
    _clubId: 0,
    _paramsTime: ['today', 'yesterday', 'three', 'week'],
    _currentIndex: 0,
    _dataList: [],
    _tableView: null,


    _startYear: null,  //开始年
    _startMonth: null, //开始月
    _startDete: null,  //开始日

    _endYear: null,  //结束年
    _endMonth: null, //结束月
    _endDete: null,  //结束日
    initItem: function (clubId) {
        this._clubId = clubId;
        this.initTableView();

        var today = new Date();
        this._startYear = this._endYear = today.getFullYear();
        this._startMonth = this._endMonth = today.getMonth() + 1;
        this._startDete = this._endDete = today.getDate();

        //设置按钮名称 用来区分点击的事那个时间按钮
        this._nodeList['dateStartNode.btn_date'].setName('startDate');
        this._nodeList['dateEndNode.btn_date'].setName('endDate');
        this._refreshDateView(this._nodeList['dateStartNode.dateLabel'], this._startYear, this._startMonth, this._startDete);
        this._refreshDateView(this._nodeList['dateEndNode.dateLabel'], this._endYear, this._endMonth, this._endDete);
    },


    _initClickListeners: function () {
        this._clickListeners = {
            'dateStartNode.btn_date': '_clickDate',
            'dateEndNode.btn_date': '_clickDate',
        }
    },
    /**
     * 初始化TableView
     */
    initTableView: function () {
        var nodeContent = this._nodeList['nodeContent'];
        var tableViewSize = nodeContent.getContentSize();
        this._tableView = new cc.TableView(this, cc.size(tableViewSize.width, tableViewSize.height));
        this._tableView.setDelegate(this);
        this._tableView.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL);
        this._tableView.setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN);
        this._tableView.setPosition(nodeContent.getPosition());
        nodeContent.getParent().addChild(this._tableView);
    },

    /**
     * 点击时间
     * @private
     */
    _clickDate: function (target) {
        var today = new Date();
        var curyear = today.getFullYear();
        var curmonth = today.getMonth() + 1;
        var curday = today.getDate();

        var dateLabel = 'dateStartNode.dateLabel';
        if (target.getName() == 'endDate') {
            dateLabel = 'dateEndNode.dateLabel';
            var _clubPowerDate = new ClubPowerDate(curyear, curmonth, curday, this._powerDateHandler.bind(this, dateLabel), [this._startDete,this._startMonth,this._startYear], null, 1);
            this.addChild(_clubPowerDate);
        } else {
            var _clubPowerDate = new ClubPowerDate(curyear, curmonth, curday, this._powerDateHandler.bind(this, dateLabel), null, null, 1);
            this.addChild(_clubPowerDate);
        }


    },
    /**
     * 时间选中回调结果
     * @private
     */
    _powerDateHandler: function (dateLabel, year, month, day) {
        if (dateLabel == 'dateStartNode.dateLabel') {
            this._startYear = year;
            this._startMonth = month;
            this._startDete = day;
        } else {
            this._endYear = year;
            this._endMonth = month;
            this._endDete = day;
        }
        this._refreshDateView(this._nodeList[dateLabel], year, month, day);
        this.requestHttp();
    },

    /**
     * 刷新时间显示
     * @private
     */
    _refreshDateView: function (label, year, month, day) {
        if (month < 10) {
            month = '0' + month;
        }
        if (day < 10) {
            day = '0' + day;
        }
        label.setString(year + '/' + month + '/' + day);
    },


    refreshView: function () {
        this.requestHttp();
        this._nodeList['card_num'].setString(gameData.numOfCards[1] + '张');
    },

    requestHttp: function () {
        showLoading('加载中...');
        var uri = '?area=' + app.area
            + '&groupId=' + this._clubId
            + '&startDate=' + this._startYear + '-' + this._startMonth + '-' + this._startDete
            + '&endDate=' + this._endYear + '-' + this._endMonth + '-' + this._endDete
            // + '&timeType=' + this._paramsTime[idx]     //今日榜today 昨日棒 yesterday // 本周榜 week
            + '&playerId=' + gameData.uid;
        cc.log('请求信息:' + uri );
        var sign = Crypto.MD5('fy-club-stat' + app.area + this._clubId + gameData.uid).toString();
        http.get(club.clubFyClubApiUrl + '/club/stat/getStat' + uri + '&sign=' + sign, this._requestHandler.bind(this));
    },
    _requestHandler: function (data) {
        hideLoading();
        var data = JSON.parse(data);
        if (data.status) {
            this._dataList = data.resultData;
            this._tableView.reloadData();
        } else {
            showAlert('' + data.errMsg);
        }
    },

    tableCellAtIndex: function (table, idx) {
        var cell = table.dequeueCell();
        var node = null;
        if (cell == null) {
            cell = new cc.TableViewCell();
            node = addItem('ClubDataOverviewItem', cell, this._dataList[idx]);
            node.setName("itemCell");
        } else {
            node = cell.getChildByName("itemCell");
            node.setData(this._dataList[idx]);
        }
        return cell;
    },
    tableCellTouched: function (table, cell) {
    },
    tableCellSizeForIndex: function (table, idx) {
        return cc.size(955, 56);
    },
    numberOfCellsInTableView: function (table) {
        return this._dataList.length;
    },


});